package cn.gong.movie.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.gong.movie.bean.Movie;
import cn.gong.movie.data.DataMovie;
import cn.gong.movie.utils.DateUtils;
import cn.gong.movie.view.ArrayWheelAdapter;
import cn.gong.movie.view.OnWheelChangedListener;
import cn.gong.movie.view.WheelView;
import framework.base.ui.BaseAct;
import framework.base.ui.ClickMethod;
import framework.base.ui.InjectView;
import framework.utils.StringUtils;

public class ActEditMovie extends BaseAct {
	@InjectView(R.id.edit_movie_et_date)
	protected TextView dateTv;
	@InjectView(R.id.edit_movie_et_name)
	protected EditText nameEt;
	@InjectView(R.id.edit_movie_ll_ok)
	protected LinearLayout okLl;

	// date wheel
	@InjectView(R.id.edit_movie_ll_wheel_date)
	private LinearLayout dateWheelLl;
	@InjectView(R.id.edit_movie_wv_year)
	private WheelView yearWv;
	@InjectView(R.id.edit_movie_wv_month)
	private WheelView monthWv;
	@InjectView(R.id.edit_movie_wv_day)
	private WheelView dayWv;
	private boolean isEdit;
	private int status = 0;
	private String movieName;
	private String date;
	private String year;
	private String month;
	private String day;

	@Override
	protected void onCreateAct(Bundle savedInstanceState) {
		super.onCreateAct(savedInstanceState);
		setContentView(R.layout.edit_movie);

		Intent intent = getIntent();
		movie = (Movie) intent.getSerializableExtra("movie");
		isEdit = (movie != null);

		if (isEdit) {
			movieName = movie.getName();
			status = movie.getStatus();
			date = movie.getDate();

			year = getYear(date);

			month = getMonth(date);

			day = getDay(date);

			setText(dateTv, year + "年" + month + "月" + day + "日");
		}

		initTitle();

		setText(nameEt, movieName);

		nameEt.setOnFocusChangeListener(cl);

	}

	private String getDay(String date2) {
		try {
			return date.substring(6);
		} catch (Exception e) {
			return "";
		}
	}

	private String getMonth(String date) {
		try {
			return date.substring(4, 6);
		} catch (Exception e) {
			return "";
		}
	}

	private String getYear(String date) {
		try {
			return date.substring(0, 4);
		} catch (Exception e) {
			return "";
		}
	}

	private OnFocusChangeListener cl = new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus) {
				viewGone(dateWheelLl);
			}
		}
	};
	private Movie movie;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (dateWheelLl.getVisibility() == View.VISIBLE) {
				viewGone(dateWheelLl);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private void initTitle() {
		TextView titleTv = findTextViewById(R.id.title_bar_tv_title);
		setText(titleTv, isEdit ? "编辑电影" : "添加电影");
		viewGone(R.id.title_bar_ib_left);
	}

	@ClickMethod(R.id.title_bar_ib_right)
	protected void clickClose(View view) {
		closeAct();
		animTranslateTopIn();
	}

	@ClickMethod(R.id.edit_movie_et_name)
	protected void clickName(View view) {
		viewGone(dateWheelLl);
	}

	@ClickMethod(R.id.edit_movie_ib_delete_name)
	protected void clickDeleteName(View view) {
		setText(nameEt, "");
	}

	@ClickMethod(R.id.edit_movie_ll_ok)
	protected void clickOk(View view) {
		String name = nameEt.getText().toString();
		if (StringUtils.isEmpty(name)) {
			toast("请输入片名");
			return;
		}

		String ymd = getYmdInt();
		log("ymd:" + ymd);
		if (StringUtils.isEmpty(ymd)) {
			toast("请选择日期");
			return;
		}

		if (movie == null) {
			movie = new Movie();
		}
		movie.setName(name);
		movie.setDate(ymd);
		movie.setStatus(status);
		if (isEdit) {
			DataMovie.updateMovie(movie, this);
		} else {
			DataMovie.setMovie(movie, this);
		}
	}

	protected void dbTsUpdateMovie(Object obj) {
		int update = (Integer) obj;
		if (update != -1) {
			Intent data = new Intent();
			data.putExtra(Strings.INTENT_EXTRA_MOVIE, movie);
			closeActForResultOk(data);
			toast("编辑成功");
		}

	}

	protected void dbTsSetMovie(Object obj) {
		long id = (Long) obj;
		if (id != -1) {
			movie.setId(id);
			Intent data = new Intent();
			data.putExtra(Strings.INTENT_EXTRA_MOVIE, movie);
			closeActForResultOk(data);
			toast("保存成功");
		}
	}

	/****************************************
	 * 
	 * 日期模块
	 * 
	 ****************************************/
	private int visibleItems = 7;
	private boolean isInitDate;
	private Boolean birthHideFlag = false;
	private ListView lv;
	private String[] yearArray;

	@ClickMethod(R.id.edit_movie_ll_date)
	protected void clickDate(View view) {
		inputHidden(nameEt);
		viewVisible(dateWheelLl);
		if (!isInitDate) {
			isInitDate = !isInitDate;
			yearArray = DateUtils.getYearArrayTotal();
			initWheelDate();
		}
	}

	private void initWheelDate() {
		DateUtils.initYmd();

		yearWv.setVisibleItems(visibleItems);
		monthWv.setVisibleItems(visibleItems);
		dayWv.setVisibleItems(visibleItems);

		yearWv.setAdapter(new ArrayWheelAdapter<String>(yearArray));
		yearWv.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				String[] monthArray = DateUtils.getMonthArrayTotal();
				monthWv.setAdapter(new ArrayWheelAdapter<String>(monthArray));
				monthWv.setCurrentItem(monthArray.length / 2);
				setText(dateTv, getYmd());
			}
		});
		monthWv.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				String[] dayArray = DateUtils.getDayArrayTotal(yearArray[yearWv.getCurrentItem()], newValue);
				dayWv.setAdapter(new ArrayWheelAdapter<String>(dayArray));
				dayWv.setCurrentItem(dayArray.length / 2);
				setText(dateTv, getYmd());
			}
		});

		dayWv.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				setText(dateTv, getYmd());
			}
		});

		yearWv.setCurrentItem(99);
	}

	private String getYmd() {
		if (birthHideFlag) {
			return getWvItem(monthWv) + getWvItem(dayWv);
		} else {
			return getWvItem(yearWv) + getWvItem(monthWv) + getWvItem(dayWv);
		}
	}

	private String getYmdInt() {
		try {
			String year = getWvItem(yearWv).substring(0, 4);
			String month = getWvItem(monthWv);
			month = month.substring(0, month.length() - 1);
			if (month.length() == 1) {
				month = "0" + month;
			}
			String day = getWvItem(dayWv);
			day = day.substring(0, day.length() - 1);
			if (day.length() == 1) {
				day = "0" + day;
			}
			return year + month + day;
		} catch (Exception e) {
			return "";
		}
	}

	private String getWvItem(WheelView wv) {
		try {
			return wv.getAdapter().getItem(wv.getCurrentItem());
		} catch (Exception e) {
			return "";
		}
	}
}
